package beusoft.ccjk.com.calculator.sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import sun.security.jca.GetInstance;

/**
 * Created by CCJK on 2017/12/16.
 */

public class StackTriangle {


    static int theNumber;
    static int theAnswer;
    static StackX theStack;
    static int codePart;
    static Params theseParams;

    public static void main(String[] args) throws IOException {
//        System.out.println("Enter a number:");
        theNumber = 6;
        rectTriangle();
        System.out.println("triangle=" + theAnswer);

    }

    private static void rectTriangle() {
        theStack = new StackX(10000);
        codePart = 1;
        while (!step()) ;

    }

    private static boolean step() {
        switch (codePart) {
            case 1:
                theseParams = new Params(theNumber, 6);
                theStack.push(theseParams);
                codePart = 2;
                break;
            case 2:
                theseParams = theStack.peek();
                if (theseParams.n == 1) {
                    theAnswer = 1;
                    codePart = 5;
                } else codePart = 3;
                break;
            case 3:
                Params newParams = new Params(theseParams.n - 1, 4);
                theStack.push(newParams);
                codePart = 2;
                break;
            case 4:
                theseParams = theStack.peek();
                theAnswer = theAnswer + theseParams.n;
                codePart = 5;
                break;
            case 5:
                theseParams = theStack.peek();
                codePart = theseParams.returnAddress;
                theStack.pop();
                break;
            case 6:
                return true;
        }
        return false;
    }

    public static int getInt() throws IOException {
//        InputStreamReader isr = new InputStreamReader(System.in);
//        BufferedReader bufferedReader = new BufferedReader(isr);
//        return Integer.parseInt(bufferedReader.readLine());
        return 3;
    }


    static class Params {
        public int n;
        public int returnAddress;

        public Params(int n, int returnAddress) {
            this.n = n;
            this.returnAddress = returnAddress;
        }
    }

    static class StackX {
        private int maxSize;
        private Params[] stackArray;
        private int top;

        public StackX(int size) {
            maxSize = size;
            stackArray = new Params[maxSize];
            top = -1;
        }

        public void push(Params p) {
            stackArray[++top] = p;
        }

        public Params pop() {
            return stackArray[top--];
        }

        public Params peek() {
            return stackArray[top];
        }

    }


}
